package net.sf.xoperator.ui.txt;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.openbbs.blackboard.filter.KindOfFilter;

import net.sf.xoperator.blackboard.NoPlanBlackBoardControl;
import net.sf.xoperator.ui.StringMessage;
import net.sf.xoperator.ui.aiml.AIMLTemplate;

public class ListAimlTemplates extends Command {

	private static final String LIST_TEMPLATES = "list templates";

	@Override
	public String getHelp() {
		return "Lists all the aiml templates in the system";
	}

	@Override
	public String getKeyWord() {
		return LIST_TEMPLATES;
	}

	@Override
	public List<Object> performInternal(String call, NoPlanBlackBoardControl control,
			Interactor interactor) {

		String returnmessage = "";
		Set aimlt = control.getConfigurationContext().blackboard().readAll(
				new KindOfFilter(AIMLTemplate.class));
		for (Object object : aimlt) {
			AIMLTemplate temp = (AIMLTemplate) object;
			if(!temp.isObsolete()){
				returnmessage += temp.getPattern() + "  " + temp.getQuery() + "\n";
			}
			

		}

		if (returnmessage.isEmpty()) {
			returnmessage = "No templates are defined";
		} else {
			returnmessage = "These are the defined templates:\n"
					+ returnmessage;
		}
		return Arrays.asList(new Object[] { new StringMessage(returnmessage) });
	}
	

	@Override
	public Pattern getValidationPattern() {

		return Pattern.compile(".*");
	}

}
